package com.example.administrator.recorddemo20181101.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.administrator.recorddemo20181101.MainActivity;
import com.example.administrator.recorddemo20181101.R;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/9.
 */

public class RecorderAdapter extends BaseAdapter {
    private int mMinItemWidth;
    private int mMaxItemWidth;
    private List<MainActivity.Recorder> mDates;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private boolean isShowCheckbox = false;

    public boolean isShowCheckbox() {
        return isShowCheckbox;
    }

    public void setShowCheckbox(boolean showCheckbox) {
        isShowCheckbox = showCheckbox;
        notifyDataSetChanged();
    }

    private Map<Integer, Boolean> mMap = new HashMap<>();

    public RecorderAdapter(@NonNull Context context, List<MainActivity.Recorder> dates) {
        mContext = context;
        mDates = dates;
        mLayoutInflater = LayoutInflater.from(mContext);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outMetrics);

        mMaxItemWidth = (int) (outMetrics.widthPixels * 0.7f);
        mMinItemWidth = (int) (outMetrics.widthPixels * 0.15f);
    }

    @Override
    public int getCount() {
        return mDates == null ? 0 : mDates.size();
    }

    @Override
    public Object getItem(int i) {
        return mDates.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_recorder, null);
            viewHolder = new ViewHolder();
            viewHolder.seconds = (TextView) convertView.findViewById(R.id.id_recorder_time);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.mLength = convertView.findViewById(R.id.id_recorder_anim);
            viewHolder.ckb = convertView.findViewById(R.id.ckb);
            viewHolder.id_recorder_length = convertView.findViewById(R.id.id_recorder_length);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int e = mDates.get(position).getMediaLength();
        Log.e("时长", e + "");
        Log.e("getMediaLength", mDates.get(position).getMediaLength() + 1 + "");
        if (Math.round(mDates.get(position).getMediaLength()) + "\"".length() == 0) {
            viewHolder.seconds.setText(Math.round(mDates.get(position).getMediaLength() + 1) + "\"");
        } else {
            viewHolder.seconds.setText(Math.round(mDates.get(position).getMediaLength()) + "\"");
        }
        viewHolder.tv_time.setText(mDates.get(position).getFileName());//时间
        if (isShowCheckbox) {
            viewHolder.ckb.setVisibility(View.VISIBLE);
        } else {
            viewHolder.ckb.setVisibility(View.GONE);
        }

        viewHolder.ckb.setChecked(mDates.get(position).isChecked());
        viewHolder.ckb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mutilChoice(position);
            }

        });
        ViewGroup.LayoutParams layoutParams = viewHolder.id_recorder_length.getLayoutParams();
        layoutParams.width = Math.min(
                (int) (mMinItemWidth + (mMaxItemWidth / 60f * mDates.get(position).getTime())), mMaxItemWidth);
        return convertView;
    }


    static class ViewHolder {
        private TextView seconds;
        private View mLength;
        private TextView tv_time;//系统时间
        private CheckBox ckb;
        private FrameLayout id_recorder_length;
    }

    /**
     * 全选
     */
    public void all() {
        for (int i = 0; i < mDates.size(); i++) {
            MainActivity.Recorder b = mDates.get(i);
            b.setChecked(true);
        }
        notifyDataSetChanged();
    }

    /**
     * 多选
     *
     * @param
     */
    public void mutilChoice(int position) {
        MainActivity.Recorder bean = mDates.get(position);
        bean.setChecked(!bean.isChecked());
        notifyDataSetChanged();
    }

    /**
     * 反选
     */
    public void reverse() {
        for (int i = 0; i < mDates.size(); i++) {
            MainActivity.Recorder b = mDates.get(i);
            //                        if (b.isChecked()) {
            //                            b.setChecked(false);
            //                        } else {
            //                            b.setChecked(true);
            //                        }
            b.setChecked(!b.isChecked());
        }
        notifyDataSetChanged();
    }

    /**
     * 获取选中的 数据
     *
     * @return
     */
    public List<MainActivity.Recorder> getCheckDatas() {
        List<MainActivity.Recorder> tempList = new ArrayList<>();
        for (MainActivity.Recorder mDate : mDates) {
            if (mDate.isChecked()) {
                tempList.add(mDate);
            }
        }
        return tempList;
    }

}
